package com.sushuo.base.bean.sql;


import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

public class PageData<T>{
    //最大每页记录数
    public static final int MAX_PAGE_SIZE=50;
    public static final int MIN_PAGE= 1;
    public static final int DEFAULT_PAGE_SIZE=20;
    public PageData(){
        this.pageSize = DEFAULT_PAGE_SIZE;
        this.currentPage = MIN_PAGE;
    }
    public PageData(Integer currentPage,Integer pageSize){
        this.pageSize= pageSize==null?DEFAULT_PAGE_SIZE:(pageSize>MAX_PAGE_SIZE?MAX_PAGE_SIZE:pageSize<1?1:pageSize);
        this.currentPage = currentPage<1?1:currentPage;
    }
    public PageData(int currentPage,int pageSize,int totalRecord){
        this(currentPage,pageSize);
        this.totalRecord= totalRecord;
    }
    private Sort sort;
    private int totalPage;  // 总页数
    private int currentPage;// 当前页数,从1开始
    private int pageSize;   // 页面大小
    private int totalRecord;// 总记录数
    private int currentRecord;// 当前返回记录数
    private List<T> data=new ArrayList<>();

    @JsonIgnore
    public Sort getSort() {
        return sort;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
        this.setCurrentRecord(null!=this.data ? this.data.size() : 0);
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setTotalPage(){
        if(this.totalRecord%pageSize==0){
            this.totalPage = totalRecord/pageSize;
        }else{
            this.totalPage = totalRecord/pageSize+1;
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }
    /**
     * 转化成jpa分页查询对象
     * @return PageRequest
     */
    public PageRequest toPageRequest(){
        return new PageRequest(currentPage-1,pageSize,sort);
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getStartRecord(){
       return (currentPage - 1) * pageSize;
    }
    
    public int getCurrentRecord() {
        return currentRecord;
    }
    public void setCurrentRecord(int currentRecord) {
        this.currentRecord = currentRecord;
    }

}
